package com.realtyManage.springboot.Controller;


import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.*;
import com.realtyManage.springboot.Service.*;
import com.realtyManage.springboot.Service.impl.HouseDetailServiceImpl;
import com.realtyManage.springboot.Utils.*;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static java.sql.Types.NULL;

/**
 * 外场渠道
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2020/8/20
 **/

@Controller
@RequestMapping("/")
public class OutfieldChannelController {

    @Autowired
    private IOutfieldChannelService outfieldChannelService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICustomerRegistrationService customerRegistrationService;

    @Autowired
    private HouseDetailServiceImpl houseDetailServiceImpl;

    @Autowired
    private IVerificationService verificationService;

    @Autowired
    private IApplyInfoService applyInfoService;

    /**
     * 新增外场渠道客户信息
     *
     * @param outfieldChannel
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addOutfieldChannel", method = RequestMethod.POST)
    private String addOutfieldChannel(OutfieldChannel outfieldChannel, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = 1;
        BaseEntity entity = new BaseEntity();
        //查询保护期时间
        //判断码是否过期
        int bhq = outfieldChannelService.queryWTime();  //获取保护期
        if (bhq > 0) {  //为0时 任何人都可以报备
            //判断号码是否已经被报备
            int countOutfieldChannelPhone = outfieldChannelService.countOutfieldChannelPhone(outfieldChannel.getPhone(), userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
            if (countOutfieldChannelPhone > 0) {
                code = 12;
                entity.setInfoCode(code);
                entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(entity);
            }
        }
        outfieldChannel.setWcUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        outfieldChannel.setWcUserName(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getName());
        outfieldChannel.setIsShow(0);
        outfieldChannel.setAlready(1);
        outfieldChannel.setStep(0);
        int countCustomer = outfieldChannelService.countCustomer(outfieldChannel.getPhone(), outfieldChannel.getWcUserId());
        if (countCustomer == 0) {
            //判断是否已经入案场库
            int countPhone = customerRegistrationService.countPhone(outfieldChannel.getPhone());
            if (countPhone < 1) {
                outfieldChannelService.save(outfieldChannel);
                code = 1;
            } else {
                code = -10087;
            }
        } else {
            code = -10087;
        }
        entity.setInfoCode(code);
        entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(entity);
    }


    /**
     * 验证客户是否重复及被其他人录入
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryOutfieldChanneVerification", method = RequestMethod.GET)
    private String queryOutfieldChanneVerification(CustomReEntity customReEntity, HttpServletRequest request) {
        BaseEntity entity = new BaseEntity();
        Integer code = 1;
        int bhq = outfieldChannelService.queryWTime();  //获取保护期
        if (bhq > 0) {  //为0时 任何人都可以报备
            //判断号码是否已经被报备
            int countCf = outfieldChannelService.countCf(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId(), customReEntity.getPhone());
            int countOutfieldChannelPhone = outfieldChannelService.countOutfieldChannelPhone(customReEntity.getPhone(), userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
            //判断是否已经入案场库
            int countPhone = customerRegistrationService.countPhone(customReEntity.getPhone());
            if (countCf > 0) {
                code = -10087;
                entity.setInfoCode(code);
                entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(entity);
            }
            if (countOutfieldChannelPhone > 0) {
                code = 12;
                entity.setInfoCode(code);
                entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(entity);
            }
            if (countPhone > 0) {
                code = -10087;
                entity.setInfoCode(code);
                entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(entity);
            }
        } else {
            int countCf = outfieldChannelService.countCf(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId(), customReEntity.getPhone());
            //判断是否已经入案场库
            int countPhone = customerRegistrationService.countPhone(customReEntity.getPhone());
            if (countCf > 0) {
                code = -10087;
                entity.setInfoCode(code);
                entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(entity);
            }
            if (countPhone > 0) {
                code = -10087;
                entity.setInfoCode(code);
                entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(entity);
            }
        }
        entity.setInfoCode(code);
        entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(entity);
    }


    /**
     * 修改外场渠道客户信息
     *
     * @param outfieldChannel
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateOutfieldChannel", method = RequestMethod.POST)
    private String updateOutfieldChannel(OutfieldChannel outfieldChannel, HttpServletRequest request) {
        Integer code = 1;
        BaseEntity entity = new BaseEntity();
        outfieldChannel.setWcUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        Integer countCustomer = outfieldChannelService.countCustomerU(outfieldChannel.getPhone(), outfieldChannel.getId());
        if (countCustomer == null || countCustomer.toString().equals(outfieldChannel.getId().toString())) {
            if (outfieldChannelService.updateOutfieldChannel(outfieldChannel) != 0) {
                code = 1;
            }
        } else {
            code = -10087;
        }
        entity.setInfoCode(code);
        entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(entity);
    }

    /**
     * 新增跟踪记录
     *
     * @param outfieldChannelRecord
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addOutfieldChannelRecord", method = RequestMethod.POST)
    private String addOutfieldChannelRecord(OutfieldChannelRecord outfieldChannelRecord, HttpServletRequest request) {
        Integer code = 1;
        BaseEntity entity = new BaseEntity();
        outfieldChannelRecord.setUid(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        outfieldChannelRecord.setInfoTime(new Date());
        if (outfieldChannelService.addOutfieldChannelRecord(outfieldChannelRecord) != 0) {
            code = 1;
        }
        entity.setInfoCode(code);
        entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(entity);
    }


    /**
     * 查看外场渠道客户列表
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryOutfieldChannelList", method = RequestMethod.GET)
    private String queryOutfieldChannelList(CustomReEntity customReEntity, HttpServletRequest request) {
        customReEntity.setReceptionId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString());
        return JSON.toJSONString(outfieldChannelService.queryOutfieldChannelList(customReEntity));
    }

    /**
     * 查看外场渠道客户人员详情
     */
    @ResponseBody
    @RequestMapping(value = "/queryOutfieldChannelById", method = RequestMethod.GET)
    private String queryOutfieldChannelById(HttpServletRequest request) {
        String id = request.getParameter("id");
        return JSON.toJSONString(outfieldChannelService.queryOutfieldChannelById(id));
    }

    /**
     * 查看公池外场客户信息
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryOutfieldChannelPool", method = RequestMethod.GET)
    private String queryOutfieldChannelPool(CustomReEntity customReEntity, HttpServletRequest request) {
        return JSON.toJSONString(outfieldChannelService.queryOutfieldChannelPool(customReEntity));
    }


    //扫码核销外场渠道报备客户
    @ResponseBody
    @RequestMapping(value = "/verificationOutfieldChannel", method = RequestMethod.POST)
    private String verificationOutfieldChannel(OutfieldChannel outfieldChannel, HttpServletRequest request) {
        Integer code = 1;
        BaseEntity entity = new BaseEntity();
        code = outfieldChannelService.verificationOutfieldChannel(outfieldChannel, request);
        entity.setInfoCode(code);
        entity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(entity);
    }


    /**
     * 人为分配外场渠道客户
     *
     * @param customReEntity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/artificialOutfieldChannel", method = RequestMethod.POST)
    private String artificialOutfieldChannel(CustomReEntity customReEntity) {
        Integer code = -1;
        RegEntity regEntity = new RegEntity();
        if (outfieldChannelService.artificialOutfieldChannel(customReEntity) > 0) {
            code = 1;
        }
        regEntity.setInfoCode(code);
        regEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regEntity);
    }


    /**
     * 系统自动分配
     *
     * @param customReEntity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/randomOutfieldChannel", method = RequestMethod.POST)
    private String randomOutfieldChannel(CustomReEntity customReEntity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String yid[] = customReEntity.getYid().split(","); //外场人员id
        String cid[] = customReEntity.getCid().split(","); //外场客户id
        ArrayList yList = new ArrayList();
        ArrayList cList = new ArrayList();
        Random random = new Random();
        for (int j = 0; j < yid.length; j++) {
            yList.add(yid[j].toString());
        }
        for (int k = 0; k < cid.length; k++) {
            cList.add(cid[k].toString());
        }
        Integer code = -1;
        RegEntity regEntity = new RegEntity();
        if (yList.size() == cList.size()) { //相等长度分配
            for (int i = 0; i < yList.size(); i++) {
                OutfieldChannel oc = new OutfieldChannel();
                OutfieldChannelRecord ocr = new OutfieldChannelRecord();
                oc.setComeTime(sdf.format(new Date()));
                oc.setWcUserId(Integer.valueOf(yList.get(i).toString()));
                oc.setWcUserName(userService.findByName(Integer.valueOf(yList.get(i).toString())));
                oc.setId(Integer.valueOf(cList.get(i).toString()));
                oc.setAlready(1);
                ocr.setUid(Integer.valueOf(yList.get(i).toString()));
                ocr.setCustomId(Integer.valueOf(cList.get(i).toString()));
                if (outfieldChannelService.updateOutfieldChannel(oc) > 0) {
                    //修改跟踪记录信息
                    outfieldChannelService.updateOutfieldChannelRecord(ocr);
                    code = 1;
                }
            }
        } else if (yList.size() > cList.size()) { //业务员大于客户数随机分配
            for (int a = 0; a < cList.size(); a++) {
                int n = random.nextInt(yList.size());
                OutfieldChannel oc = new OutfieldChannel();
                OutfieldChannelRecord ocr = new OutfieldChannelRecord();
                oc.setComeTime(sdf.format(new Date()));
                oc.setWcUserId(Integer.valueOf(yList.get(n).toString()));
                oc.setWcUserName(userService.findByName(Integer.valueOf(yList.get(n).toString())));
                oc.setId(Integer.valueOf(cList.get(a).toString()));
                oc.setAlready(1);
                //修改来访信息
                ocr.setUid(Integer.valueOf(yList.get(n).toString()));
                ocr.setCustomId(Integer.valueOf(cList.get(a).toString()));
                if (outfieldChannelService.updateOutfieldChannel(oc) > 0) {
                    outfieldChannelService.updateOutfieldChannelRecord(ocr);
                    code = 1;
                }
                yList.remove(n);
            }
        } else { //业务员小于客户数
            for (int b = 0; b < cList.size(); b++) {
                int n = random.nextInt(yList.size());
                OutfieldChannel oc = new OutfieldChannel();
                OutfieldChannelRecord ocr = new OutfieldChannelRecord();
                oc.setComeTime((sdf.format(new Date())));
                oc.setWcUserId(Integer.valueOf(yList.get(n).toString()));
                oc.setWcUserName(userService.findByName(Integer.valueOf(yList.get(n).toString())));
                oc.setId(Integer.valueOf(cList.get(b).toString()));
                oc.setAlready(1);
                //修改来访信息
                ocr.setUid(Integer.valueOf(yList.get(n).toString()));
                ocr.setCustomId(Integer.valueOf(cList.get(b).toString()));
                if (outfieldChannelService.updateOutfieldChannel(oc) > 0) {
                    outfieldChannelService.updateOutfieldChannelRecord(ocr);
                    code = 1;
                }
            }
        }
        regEntity.setInfoCode(code);
        regEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regEntity);
    }


    /**
     * 获取外场置业顾问
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getWRoleUser", method = RequestMethod.GET)
    private String getWRoleUser(HttpServletRequest request) {
        return JSON.toJSONString(userService.getWRoleUser());
    }


    /**
     * 小程序查看外场渠道客户列表信息
     *
     * @param applyEntity
     * @param request
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/queryOutfieldChannelXcx", method = RequestMethod.GET)
    private String queryOutfieldChannelXcx(ApplyEntity applyEntity, HttpServletRequest request) throws ParseException {
        applyEntity.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId());
        outfieldChannelService.queryApplyInfoS(applyEntity);
        return JSON.toJSONString(outfieldChannelService.queryApplyInfoE(applyEntity));
    }


    /**
     * 判断是否名称重复
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryOutfieldChannelCm", method = RequestMethod.GET)
    private String queryOutfieldChannelCm(CustomReEntity customReEntity, HttpServletRequest request) {
        Integer code = 1;
        RegEntity regEntity = new RegEntity();
        if (outfieldChannelService.queryOutfieldChannelCm(customReEntity) > 0) {
            code = -1;
        }
        regEntity.setInfoCode(code);
        regEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regEntity);
    }


    /**
     * 查看审核信息
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryShCustomerXkDetail", method = RequestMethod.GET)
    private String queryShCustomerXkDetail(CustomReEntity customReEntity, HttpServletRequest request) {
        Integer code = 1;
        RegEntity regEntity = new RegEntity();
        if (outfieldChannelService.queryOutfieldChannelCm(customReEntity) > 0) {
            code = -1;
        }
        regEntity.setInfoCode(code);
        regEntity.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regEntity);
    }


    /**
     * 获取外渠道人员
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryWSalesperson", method = RequestMethod.GET)
    private String queryWSalesperson(CustomReEntity customReEntity, HttpServletRequest request) {
        return JSON.toJSONString(customerRegistrationService.queryWSalesperson());
    }


    /**
     * 客户销售记信息选择继续跟踪时，此销售成交信息清空
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/emptyRecordHx", method = RequestMethod.POST)
    @ResponseBody
    private String emptyRecordHx(CustomerRegistration customerRegistration, HttpServletRequest request) {
        RegEntity regInfo = new RegEntity();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer code = -1;
        HouseDetail hd = houseDetailServiceImpl.queryHouseHxType(customerRegistration.getDh(), customerRegistration.getDyh(), customerRegistration.getMph());
        if (hd.getRelatedParty() != null) {
            if (!hd.getRelatedParty().toString().equals(customerRegistration.getId().toString())) {
                code = -10086;
                regInfo.setInfoCode(code);
                regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                return JSON.toJSONString(regInfo);
            }
        } else {
            HouseDetail h = new HouseDetail();
            h.setId(hd.getId());
            h.setArchitectureArea(customerRegistration.getHouseArea());
            h.setOldTotal(customerRegistration.getHouseTotalPrice());
            h.setRelatedParty(customerRegistration.getId());
            h.setRelatedAdviser(customerRegistration.getReceptionId());
            //清除之前的房源信息
            houseDetailServiceImpl.clear(customerRegistration.getId());
            houseDetailServiceImpl.updateHouseInfo(h);
        }
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 导出渠道外场客户信息
     *
     * @param customReEntity
     * @param response
     * @param request
     */
    @RequestMapping(value = "/exportWcCustomerData", method = {RequestMethod.GET, RequestMethod.POST})
    public void exportWcCustomerData(CustomReEntity customReEntity, HttpServletResponse response, HttpServletRequest request) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        //xls格式最大存储行数
        Integer max = 65535;
        customReEntity.setPageNumber(1);
        customReEntity.setPageSize(max);
        List<OutfieldChannel> list = new ArrayList<>();
        List<String> header = new ArrayList<>();
        List<String> field = new ArrayList<>();
        String tmpName = null;
        list = outfieldChannelService.exportWcCustomerData(customReEntity).getContent();
        //excel表格头
        header.add("姓名");
        header.add("电话");
        header.add("外场人员姓名");
        header.add("详细地址");

        header.add("客户性别");
        header.add("年龄");
        header.add("居住区域");
        header.add("意向物业类型");

        header.add("获知途径");
        header.add("面积需求");
        header.add("总价需求");
        header.add("置业次数");

        header.add("付款方式");
        header.add("户型需求");
        header.add("考虑因素");
        header.add("客户职业");

        header.add("家庭结构");
        header.add("购房目的");
        header.add("客户评级");
        header.add("关联步骤");

        header.add("是否看过样板房");
        header.add("价格接受度");
        header.add("跟踪记录");

        //对应实体字段
        field.add("name");
        field.add("phone");
        field.add("wcUserName");
        field.add("address");

        field.add("xb");
        field.add("age");
        field.add("residentialArea");
        field.add("intentionPropertyType");

        field.add("accessKnowledge");
        field.add("areaNeed");
        field.add("totalNeed");
        field.add("homeOwnership");

        field.add("paymentType");
        field.add("houseTypeNeed");
        field.add("considerType");
        field.add("customerOccupation");

        field.add("familyStructure");
        field.add("purchasePurpose");
        field.add("level");
        field.add("lx");

        field.add("modelHouses");
        field.add("priceAcceptance");
        field.add("jl");

        HSSFWorkbook xssfSheets = ExcelUtil.exportOutfieldChannelExcel(list, header.toArray(new String[0]), field.toArray(new String[0]), "渠道客户表信息");
        HSSFCellStyle style = xssfSheets.createCellStyle();
        HSSFSheet sheet = xssfSheets.createSheet();// 创建一个Excel的Sheet
        style.setWrapText(true);//自动换行
        sheet.setColumnWidth(24, 2500);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        OutputStream outputStream = null;
        StringBuilder sb = new StringBuilder();
        try {
            xssfSheets.write(os);
            byte[] bytes = os.toByteArray();
            response.reset();
            response.setContentType("application/ms-excel;charset=UTF-8");
            sb.append("外场渠道客户表信息" + df.format(new Date()));
            sb.append(".xls");
            String filename = URLEncoder.encode(sb.toString(), "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 查询PC端外场渠道客户列表
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryPCOutfieldChannelList", method = RequestMethod.GET)
    private String queryPCOutfieldChannelList(CustomReEntity customReEntity, HttpServletRequest request) {
        return JSON.toJSONString(outfieldChannelService.queryPCOutfieldChannelList(customReEntity));
    }


    /**
     * 查询移动端外场渠道客户列表
     *
     * @param customReEntity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryYDOutfieldChannelList", method = RequestMethod.GET)
    private String queryYDOutfieldChannelList(CustomReEntity customReEntity, HttpServletRequest request) {
        return JSON.toJSONString(outfieldChannelService.queryYDOutfieldChannelList(customReEntity));
    }


    /**
     * 获取核销客户信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryHxCustomer", method = RequestMethod.GET)
    private String queryHxCustomer(CustomReEntity customReEntity, HttpServletRequest request) {
        List<String> lst = outfieldChannelService.queryHxCustomer(customReEntity);
        return JSON.toJSONString(lst);
    }


    /**
     * 核销-》客户销售信息
     *
     * @param hxCustomEntity
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifyHxCustomer", method = RequestMethod.POST)
    @ResponseBody
    private String modifyHxCustomer(HxCustomEntity hxCustomEntity, HttpServletRequest request) {
        RegEntity regInfo = new RegEntity();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Verification v = new Verification();
        Integer code = 1;
        CustomerRegistration cr = new CustomerRegistration();
        cr.setId(Integer.valueOf(hxCustomEntity.getCustomId()));
        cr.setCustomerType(hxCustomEntity.getCustomType());
        v.setId(Integer.valueOf(hxCustomEntity.getId()));
        v.setYh(hxCustomEntity.getMoneyBack());
        v.setBlockCode(Integer.valueOf(hxCustomEntity.getBlockCode()));
        v.setUnitCode(hxCustomEntity.getUnitCode());
        v.setDoorplateCode(hxCustomEntity.getDoorplateCode());
        v.setTjType(hxCustomEntity.getCustomType());
        //判断状态
        if (hxCustomEntity.getCustomType().equals("继续跟踪")) {
            //修改客户释放房源
            customerRegistrationService.updateBh(hxCustomEntity.getCustomId());
            customerRegistrationService.updateJxHxc(hxCustomEntity.getCustomId());
            verificationService.updateVerificationStep(hxCustomEntity.getId());
            verificationService.updateVerification(v);
        } else if (hxCustomEntity.getCustomType().equals("已认筹/已交定")) {
            HouseDetail hd = houseDetailServiceImpl.getBlockCodeY(hxCustomEntity.getBlockCode(), hxCustomEntity.getUnitCode(), hxCustomEntity.getDoorplateCode());
            if (hd != null) {   //判断是否被核销
                if (!hd.getType().equals("0")) {
                    code = -10086;
                    regInfo.setInfoCode(code);
                    regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                    return JSON.toJSONString(regInfo);
                }
            }
            HouseDetail houseDetail = new HouseDetail();
            houseDetail.setId(hd.getId());
            houseDetail.setType("3");
            houseDetail.setArtificial(3);
            houseDetail.setTotalActualAmount(hxCustomEntity.getTotalActualAmount());
            houseDetail.setRelatedParty(Integer.valueOf(hxCustomEntity.getCustomId()));
            houseDetail.setRelatedAdviser(Integer.valueOf(hxCustomEntity.getRelatedAdviserId()));
            houseDetail.setCjTime(sdf.format(new Date()));
            //修改房源面积，房源总价
            houseDetail.setOldTotal(hxCustomEntity.getDiscountTotal());
            houseDetail.setArchitectureArea(hxCustomEntity.getArchitectureArea());
            houseDetailServiceImpl.updateHouseInfo(houseDetail);    //修改房源信息

            if (!hxCustomEntity.getTotalActualAmount().equals("0") && !hxCustomEntity.getTotalActualAmount().equals("")) {
                cr.setCjc(1);
            }
            cr.setMoneyBack(hxCustomEntity.getMoneyBack());
            cr.setLargeSumMoney(hxCustomEntity.getMoneyBack());
            customerRegistrationService.updateCjHxc(hxCustomEntity.getCustomId());
            customerRegistrationService.updateComoCustom(cr);
            //修改其它申请相同房源信息
            List<Verification> vv = verificationService.findXtVerification(hxCustomEntity.getBlockCode(), hxCustomEntity.getUnitCode(), hxCustomEntity.getDoorplateCode(), hxCustomEntity.getId());
            if (vv != null) {
                for (int i = 0; i < vv.size(); i++) {
                    customerRegistrationService.updateBh(vv.get(i).getcId().toString());
                }
            }
            //查询是否与外场渠道有关联
            CustomerRegistration customerRegistration = customerRegistrationService.findWc(Integer.valueOf(hxCustomEntity.getCustomId()));
            if (customerRegistration.getWcUserId() != null && customerRegistration.getWcUserId() != ' ') {
                ApplyInfo ai = applyInfoService.queryApplyInfo(customerRegistration.getVirtualNumber());
                OutfieldChannel oc = outfieldChannelService.queryOutfieldChannelByVirtualNumber(customerRegistration.getVirtualNumber());
                if (oc != null) {
                    if (!hxCustomEntity.getTotalActualAmount().equals("0") && !hxCustomEntity.getTotalActualAmount().equals("")) {
                        outfieldChannelService.updateStepW(oc.getId(), 3);
                    } else {
                        outfieldChannelService.updateStepW(oc.getId(), 1);
                    }
                } else if (ai != null) {
                    if (!hxCustomEntity.getTotalActualAmount().equals("0") && !hxCustomEntity.getTotalActualAmount().equals("")) {
                        applyInfoService.updateStepZ(ai.getId(), 3);
                    } else {
                        applyInfoService.updateStepZ(ai.getId(), 1);
                    }
                }
            }
            verificationService.updateVerificationStep(hxCustomEntity.getId());
            verificationService.updateVerification(v);
            code = 1;
        } else if (hxCustomEntity.getCustomType().equals("签约已首付/已全款")) {
            HouseDetail h = houseDetailServiceImpl.getBlockCodeY(hxCustomEntity.getBlockCode(), hxCustomEntity.getUnitCode(), hxCustomEntity.getDoorplateCode());
            if (h != null) {   //判断是否被核销
                if (!h.getType().equals("0")) {
                    code = -10086;
                    regInfo.setInfoCode(code);
                    regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
                    return JSON.toJSONString(regInfo);
                }
            }
            HouseDetail houseDetail = new HouseDetail();
            houseDetail.setId(h.getId());
            houseDetail.setType("4");
            houseDetail.setTotalActualAmount(hxCustomEntity.getTotalActualAmount());
            houseDetail.setRelatedParty(Integer.valueOf(hxCustomEntity.getCustomId()));
            houseDetail.setArtificial(3);
            houseDetail.setRelatedAdviser(Integer.valueOf(hxCustomEntity.getRelatedAdviserId()));
            houseDetail.setCjTime(sdf.format(new Date()));
            //修改房源面积，房源总价
            houseDetail.setOldTotal(hxCustomEntity.getDiscountTotal());
            houseDetail.setArchitectureArea(hxCustomEntity.getArchitectureArea());
            houseDetailServiceImpl.updateHouseInfo(houseDetail);    //修改房源信息
            if (!hxCustomEntity.getTotalActualAmount().equals("0") && !hxCustomEntity.getTotalActualAmount().equals("")) {
                cr.setCjc(1);
            }
            cr.setMoneyBack(hxCustomEntity.getMoneyBack());
            cr.setLargeSumMoney(hxCustomEntity.getMoneyBack());
            customerRegistrationService.updateComoCustom(cr);
            customerRegistrationService.updateCjHxc(hxCustomEntity.getCustomId());
            //修改其它申请相同房源信息
            List<Verification> vv = verificationService.findXtVerification(hxCustomEntity.getBlockCode(), hxCustomEntity.getUnitCode(), hxCustomEntity.getDoorplateCode(), hxCustomEntity.getId());
            if (vv != null) {
                for (int i = 0; i < vv.size(); i++) {
                    customerRegistrationService.updateBh(vv.get(i).getcId().toString());
                }
            }

            //查询是否与外场渠道有关联
            CustomerRegistration customerRegistration = customerRegistrationService.findWc(Integer.valueOf(hxCustomEntity.getCustomId()));
            if (customerRegistration.getWcUserId() != null && customerRegistration.getWcUserId() != ' ') {
                ApplyInfo ai = applyInfoService.queryApplyInfo(customerRegistration.getVirtualNumber());
                OutfieldChannel oc = outfieldChannelService.queryOutfieldChannelByVirtualNumber(customerRegistration.getVirtualNumber());
                if (oc != null) {
                    if (!hxCustomEntity.getTotalActualAmount().equals("0") && !hxCustomEntity.getTotalActualAmount().equals("")) {
                        outfieldChannelService.updateStepW(oc.getId(), 3);
                    } else {
                        outfieldChannelService.updateStepW(oc.getId(), 1);
                    }
                } else if (ai != null) {
                    if (!hxCustomEntity.getTotalActualAmount().equals("0") && !hxCustomEntity.getTotalActualAmount().equals("")) {
                        applyInfoService.updateStepZ(ai.getId(), 3);
                    } else {
                        applyInfoService.updateStepZ(ai.getId(), 1);
                    }
                }
            }
            verificationService.updateVerificationStep(hxCustomEntity.getId());
            verificationService.updateVerification(v);
        } else if (hxCustomEntity.getCustomType().equals("明确拒绝")) {
            cr.setPaymentType("");
            cr.setPaymentTypeMoney("0");
            cr.setSmallSum(0);
            cr.setSmallSumMoney("0");
            cr.setDownPayments(0);
            cr.setDownPaymentsMoney("0");
            cr.setLargeSum(0);
            cr.setLargeSumMoney("0");
            cr.setIdentify(0);
            cr.setIdentifyMoney("0");
            cr.setMoneyBack("0");
            cr.setIdentify(0);
            cr.setIdentifyMoney("0");
            cr.setCjc(0);
            cr.setComeTime(sdf.format(new Date()));
            cr.setCjj("NULL");
            cr.setDh(NULL);
            cr.setDyh("NULL");
            cr.setMph("NULL");
            cr.setReceptionId(0);
            cr.setCustomerType("继续跟踪");
            customerRegistrationService.updateComoCustom(cr);
            //查询是否与外场渠道有关联
            CustomerRegistration customerRegistration = customerRegistrationService.findWc(Integer.valueOf(hxCustomEntity.getCustomId()));
            //查询是否连带经纪人信息
            OutfieldChannel oc = outfieldChannelService.queryOutfieldChannelByVirtualNumber(customerRegistration.getVirtualNumber());  //外场渠道连带信息
            ApplyInfo ai = applyInfoService.queryApplyInfo(customerRegistration.getVirtualNumber());
            if (oc != null) {
                outfieldChannelService.updateStepW(oc.getId(), 4);
            } else if (ai != null) {
                applyInfoService.updateStepZ(ai.getId(), 4);
            }
            houseDetailServiceImpl.updateHouseTypeN(customerRegistration.getId());
            verificationService.updateVerification(v);
            code = 1;
        }
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }
}
